const { defineConfig } = require('@vue/cli-service')
const path = require('path')

// 代理设置
const { proxy } = require('./proxy')

// const { name } = require('./src/setting')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const port = process.env.port || process.env.npm_config_port || 9527 // dev port

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: process.env.NODE_ENV === 'development' ? '/' : process.env.VUE_APP_ROOT_PATH,
  outputDir: 'dist',
  assetsDir: 'static',
  productionSourceMap: false, // 生产环境不需要map文件
  devServer: {
    port: port,
    open: false,
    client: {
      overlay: {
        warnings: false,
        errors: true
      }
    }
    // proxy: proxy
  },
  configureWebpack: {
    // name,
    resolve: {
      extensions: ['.js', '.vue', '.ts'],
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack(config) {
    config.set('name', '直播点播')

    config.plugin('html').tap((args) => {
      args[0].title = '直播点播'
      args[0].dateTime = new Date().getTime()
      return args
    })

    config.when(process.env.NODE_ENV !== 'development', (config) => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          elementUI: {
            name: 'chunk-elementUI',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-plus(.*)/
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
    })

    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/svgs'))
      .end()

    config.module
      .rule('svg-sprite-loader')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/svgs'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'svg-icon-[name]'
      })
      .end()
  }
})
